package com.example.nick.bodyapplication

import android.R
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.PRIORITY_MAX
//import com.example.nick.bodyapplication.NotificationHelper.SECONDARY_CHANNEL
import com.example.nick.bodyapplication.databinding.ActivityMainBinding
import com.example.nick.leg.LegManager

class MainActivity : AppCompatActivity() {

    lateinit var viewBinding: ActivityMainBinding
//    private var noti: NotificationHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        var file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
        Log.i("file", "file: ${file.absolutePath}, ${file.canonicalPath}, ${file.path}, ${file.invariantSeparatorsPath}, ${file.toString()}")
        viewBinding.tvHello.setOnClickListener {
            var str = LegManager.sayHello() + ": ${Math.random()}"
            viewBinding.tvHello.text = str
            if (viewBinding.tvShow.visibility == View.GONE) {
                viewBinding.tvShow.visibility = View.VISIBLE
            } else {
                viewBinding.tvShow.visibility = View.GONE
            }

//            showNotification(Math.random())
//            sendNotification(NOTI_SECONDARY2, "2222")
//            mySendNoti()
            showNotification(22)
            viewBinding.root.invalidate()
        }
//        noti = NotificationHelper(this)
        viewBinding.tvShow.setOnClickListener { goToNotificationSettings(SECONDARY_CHANNEL) }
    }

    private val NOTI_STAVIX_ID = 1444
    val SECONDARY_CHANNEL = "stavix—disconnect222"
    private fun showNotification(ipccListIndex: Int) {
        this.let {
            val manager = it.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val chan2 = NotificationChannel(
                    SECONDARY_CHANNEL,
                    "stavix—disconnect",
                    NotificationManager.IMPORTANCE_HIGH
                )
                manager.createNotificationChannel(chan2)
                var nb: Notification.Builder = Notification.Builder(it, SECONDARY_CHANNEL)
                    .setContentTitle("Title: name")
                    .setContentText("连接失败  $ipccListIndex")
                    .setSmallIcon(R.drawable.stat_notify_chat)
                    .setAutoCancel(true)
                manager.notify(NOTI_STAVIX_ID, nb.build())
            } else {
//                val notificationBuilder: NotificationCompat.Builder =
//                    NotificationCompat.Builder(this, channelId)
//                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setContentTitle(notification.getTitle())
//                        .setContentText(notification.getBody())
//                        .setAutoCancel(true)
//                        .setShowWhen(true)
//                        .setContentIntent(pendingIntent)
                val builder = NotificationCompat.Builder(this@MainActivity)
                    .setContentTitle("Title: name")
                    .setContentText("连接失败  $ipccListIndex")
                    .setSmallIcon(R.drawable.stat_notify_chat)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setAutoCancel(true)
                builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                var nb: Notification.Builder = Notification.Builder(it)
                    .setContentTitle("Title: name")
                    .setContentText("连接失败  $ipccListIndex")
                    .setSmallIcon(R.drawable.stat_notify_chat)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setAutoCancel(true)
//                manager.notify(NOTI_STAVIX_ID, nb.build())
                manager.notify(NOTI_STAVIX_ID, nb.build())
            }
        }
    }

//    private val NOTIFY_ID = 110
    private val NOTI_SECONDARY2 = 1201
    private fun showNotification(ipccListIndex: Double) {
        this.let {

            //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            val hangIntent = Intent(it, MainActivity::class.java)
            val pendingIntent = PendingIntent.getActivity(
                it, 0 /* Request code */, hangIntent,
                PendingIntent.FLAG_ONE_SHOT
            )

            val channelId = "msg id to ChannelId"
            val notificationBuilder: NotificationCompat.Builder =
                NotificationCompat.Builder(it, channelId)
                    .setSmallIcon(R.drawable.ic_btn_speak_now)
                    .setContentTitle("Title: $ipccListIndex")
                    .setContentText("Body: $ipccListIndex")
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(PRIORITY_MAX)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setCategory(Notification.CATEGORY_ALARM)
                    .setAutoCancel(true)
                    .setShowWhen(true)
                    .setContentIntent(pendingIntent)

            val notificationManager = it.getSystemService(NOTIFICATION_SERVICE) as NotificationManager?
            // Since android Oreo notification channel is needed.
            //需要在创建CHANNEL时确定
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(
                    channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH
                )
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC)
                notificationManager!!.createNotificationChannel(channel)
            } else {
                notificationBuilder.setSound(null)
            }
            notificationManager!!.notify(
                NOTI_SECONDARY2 /* ID of notification */,
                notificationBuilder.build()
            )
        }
    }

//    fun sendNotification(id: Int, title: String?) {
//        var nb: Notification.Builder = noti!!.getNotification2(title, "secondary2_body")
//        noti!!.notify(id, nb)
//    }

    fun goToNotificationSettings(channel: String?) {
        val i = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
        i.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
        i.putExtra(Settings.EXTRA_CHANNEL_ID, channel)
        startActivity(i)
    }

//    fun mySendNoti() {
//        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//        val chan2 = NotificationChannel(
//            NotificationHelper.SECONDARY_CHANNEL,
//            getString(com.example.nick.bodyapplication.R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH
//        )
////        chan2.lightColor = Color.BLUE
////        chan2.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
//        manager.createNotificationChannel(chan2)
//
//        var nb: Notification.Builder = Notification.Builder(getApplicationContext(), SECONDARY_CHANNEL)
//            .setContentTitle(title)
//            .setContentText("My body 222")
//            .setSmallIcon(R.drawable.stat_notify_chat)
//            .setAutoCancel(true)
//        manager.notify(NOTI_SECONDARY2, nb.build())
//    }

}